<template>
    <div>
        <Card shadow>
            <div class="search-con search-con-top">
                <ButtonGroup>
                    <Button :disabled="hasAuthority('OrderStats2Edit}')?false:true"  class="search-btn" type="primary" @click="handleModal()">
                        <span>添加</span>
                    </Button>
                </ButtonGroup>
            </div>
            <Table :columns="columns" :data="data" :loading="loading">
                <template slot="action" slot-scope="{ row }">
                    <a :disabled="hasAuthority('OrderStats2Edit')?false:true"   @click="handleModal(row)">
                        编辑</a>&nbsp;
                    <Dropdown v-show="hasAuthority('OrderStats2Edit')" transfer ref="dropdown" @on-click="handleClick($event,row)">
                        <a href="javascript:void(0)">
                            <span>更多</span>
                            <Icon type="ios-arrow-down"></Icon>
                        </a>
                        <DropdownMenu slot="list">
                            <DropdownItem  name="remove">删除</DropdownItem>
                        </DropdownMenu>
                    </Dropdown>&nbsp;
                </template>
            </Table>
            <Page :total="pageInfo.total" :current="pageInfo.page" :page-size="pageInfo.limit" show-elevator show-sizer
                  show-total
                  @on-change="handlePage" @on-page-size-change='handlePageSize'></Page>
        </Card>
        <Modal v-model="modalVisible"
               :title="modalTitle"
               width="40"
               @on-cancel="handleReset">
            <div>
                <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="100">
                    <FormItem label="门店id" prop="storeId">
                        <Input v-model="formItem.storeId"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="年份" prop="year">
                        <Input v-model="formItem.year"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="月份" prop="month">
                        <Input v-model="formItem.month"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="日期" prop="day">
                        <Input v-model="formItem.day"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="营业总额，单位分" prop="amount">
                        <Input v-model="formItem.amount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="订单笔数" prop="count">
                        <Input v-model="formItem.count"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="核销总金额，单位分" prop="writeoffAmount">
                        <Input v-model="formItem.writeoffAmount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="核销笔数" prop="writeoffCount">
                        <Input v-model="formItem.writeoffCount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="现金营业额，单位分" prop="cashAmount">
                        <Input v-model="formItem.cashAmount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="现金笔数" prop="cashCount">
                        <Input v-model="formItem.cashCount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="刷卡营业额，单位分" prop="swipeAmount">
                        <Input v-model="formItem.swipeAmount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="刷卡笔数" prop="swipeCount">
                        <Input v-model="formItem.swipeCount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="线上营业额，单位分" prop="onlineAmount">
                        <Input v-model="formItem.onlineAmount"  placeholder="请输入内容"></Input>
                    </FormItem>
                    <FormItem label="线上笔数" prop="onlineCount">
                        <Input v-model="formItem.onlineCount"  placeholder="请输入内容"></Input>
                    </FormItem>
                </Form>
                <div class="drawer-footer">
                    <Button type="default" @click="handleReset">取消</Button>&nbsp;
                    <Button type="primary" @click="handleSubmit" :loading="saving">保存</Button>
                </div>
            </div>
        </Modal>
    </div>
</template>

<script>
    import OrderStats2 from '@/api/OrderStats2'

    export default {
        name: 'OrderStats2',
        data () {
            return {
                loading: false,
                saving: false,
                modalVisible: false,
                modalTitle: '',
                pageInfo: {
                    total: 0,
                    page: 1,
                    limit: 10
                },
                formItemRules: {
                    storeId: [
                        {required: true, message: '门店id不能为空', trigger: 'blur'}
                    ],
                    year: [
                        {required: true, message: '年份不能为空', trigger: 'blur'}
                    ],
                    month: [
                        {required: true, message: '月份不能为空', trigger: 'blur'}
                    ],
                    day: [
                        {required: true, message: '日期不能为空', trigger: 'blur'}
                    ],
                    amount: [
                        {required: true, message: '营业总额，单位分不能为空', trigger: 'blur'}
                    ],
                    count: [
                        {required: true, message: '订单笔数不能为空', trigger: 'blur'}
                    ],
                    writeoffAmount: [
                        {required: true, message: '核销总金额，单位分不能为空', trigger: 'blur'}
                    ],
                    writeoffCount: [
                        {required: true, message: '核销笔数不能为空', trigger: 'blur'}
                    ],
                    cashAmount: [
                        {required: true, message: '现金营业额，单位分不能为空', trigger: 'blur'}
                    ],
                    cashCount: [
                        {required: true, message: '现金笔数不能为空', trigger: 'blur'}
                    ],
                    swipeAmount: [
                        {required: true, message: '刷卡营业额，单位分不能为空', trigger: 'blur'}
                    ],
                    swipeCount: [
                        {required: true, message: '刷卡笔数不能为空', trigger: 'blur'}
                    ],
                    onlineAmount: [
                        {required: true, message: '线上营业额，单位分不能为空', trigger: 'blur'}
                    ],
                    onlineCount: [
                        {required: true, message: '线上笔数不能为空', trigger: 'blur'}
                    ],
                },
                formItem: {
                    storeId: '',
                    year: '',
                    month: '',
                    day: '',
                    amount: '',
                    count: '',
                    writeoffAmount: '',
                    writeoffCount: '',
                    cashAmount: '',
                    cashCount: '',
                    swipeAmount: '',
                    swipeCount: '',
                    onlineAmount: '',
                    onlineCount: '',
                },
                columns: [
                    {
                        title: '门店id',
                        key: 'storeId',
                        width: 100
                    },
                    {
                        title: '年份',
                        key: 'year',
                        width: 100
                    },
                    {
                        title: '月份',
                        key: 'month',
                        width: 100
                    },
                    {
                        title: '日期',
                        key: 'day',
                        width: 100
                    },
                    {
                        title: '营业总额，单位分',
                        key: 'amount',
                        width: 100
                    },
                    {
                        title: '订单笔数',
                        key: 'count',
                        width: 100
                    },
                    {
                        title: '核销总金额，单位分',
                        key: 'writeoffAmount',
                        width: 100
                    },
                    {
                        title: '核销笔数',
                        key: 'writeoffCount',
                        width: 100
                    },
                    {
                        title: '现金营业额，单位分',
                        key: 'cashAmount',
                        width: 100
                    },
                    {
                        title: '现金笔数',
                        key: 'cashCount',
                        width: 100
                    },
                    {
                        title: '刷卡营业额，单位分',
                        key: 'swipeAmount',
                        width: 100
                    },
                    {
                        title: '刷卡笔数',
                        key: 'swipeCount',
                        width: 100
                    },
                    {
                        title: '线上营业额，单位分',
                        key: 'onlineAmount',
                        width: 100
                    },
                    {
                        title: '线上笔数',
                        key: 'onlineCount',
                        width: 100
                    },
                    {
                        title: '操作',
                        slot: 'action',
                        fixed: 'right',
                        width: 120
                    }
                ],
                data: []
            }
        },
        methods: {
            handleModal (data) {
                if (data) {
                    this.modalTitle = '编辑门店营销额统计，维度：门店，年，月，日'
                    this.formItem = Object.assign({}, this.formItem, data)
                } else {
                    this.modalTitle = '添加门店营销额统计，维度：门店，年，月，日'
                }
                this.modalVisible = true
            },
            handleReset () {
                const newData = {
                    storeId: '',
                    year: '',
                    month: '',
                    day: '',
                    amount: '',
                    count: '',
                    writeoffAmount: '',
                    writeoffCount: '',
                    cashAmount: '',
                    cashCount: '',
                    swipeAmount: '',
                    swipeCount: '',
                    onlineAmount: '',
                    onlineCount: '',
                }
                this.formItem = newData
                //重置验证
                let form  = this.refs['form']
                form.resetFields()
                        this.modalVisible = false
                this.saving = false
            },
            handleSubmit () {
                let form  = this.refs['form']
                form.validate((valid) => {
                    if (valid) {
                        this.saving = true
                        if (this.formItem.$id) {
                        OrderStats2.update(this.formItem).then(res => {
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                                this.handleReset()
                             }
                            this.handleSearch()
                        }).finally(() => {this.saving = false})
                    } else {
                       OrderStats2.add(this.formItem).then(res => {
                            this.handleReset()
                            this.handleSearch()
                            if (res.code === 0) {
                                this.$Message.success('保存成功')
                            }
                          }).finally(() => {this.saving = false})
                        }
                   }
                })
            },
            handleSearch (page) {
                if (page) {
                    this.pageInfo.page = page
                }
                this.loading = true
                OrderStats2.list({page: this.pageInfo.page, limit: this.pageInfo.limit}).then(res => {
                    this.data = res.data.records
                    this.pageInfo.total = parseInt(res.data.total)
                }).finally(() => {this.loading = false})
            },
            handlePage (current) {
                this.pageInfo.page = current
                this.handleSearch()
            },
            handlePageSize (size) {
                this.pageInfo.limit = size
                this.handleSearch()
            },
            handleRemove (data) {
               let modal = this.$Modal
               modal.confirm({
                    title: '确定删除吗？',
                    onOk: () => {
                        OrderStats2.remove(data.$id).then(res => {
                            if (res.code === 0) {
                                this.pageInfo.page = 1;
                                this.$Message.success('删除成功');
                            }
                            this.handleSearch();
                        })
                    }
                })
            },
            handleClick (name, row) {
                switch (name) {
                    case 'remove':
                        this.handleRemove(row)
                        break
                }
            }
        },
        mounted: function () {
            this.handleSearch()
        }
    }
</script>
